<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>类的基本知识</title>
</head>
<body>
    <script type="text/javascript">
        class Person {
            constructor(name, age) {
                this.name = name;
                this.age = age;
            }
            speak() {
                console.log(`我叫${this.name}, 我的年龄是${this.age}`);
            }
        }

        class Dog {
            constructor(name, age) {
                this.name = name;
                this.age = age;
            }
        }

        const p1 = new Person("zs", 12);
        const p2 = new Person("lisi", 19);
        // p1.speak();
        // p2.speak();
        //
        // let d1 = new Dog("小花", 2);
        // p1.speak.call(d1);      // 我叫小花, 我的年龄是2

        // 继承
        class Student extends Person {
            constructor(name, age, grade) {
                super(name, age);
                this.grade = grade;
            }

            // 重写父类的speak方法
            speak() {
                console.log(`我叫${this.name}, 我的年龄是${this.age},我在上${this.grade}`);
                this.study();
            }

            study() {
                // study 方法放在哪里? ---- 类的原型对象上, 供实例使用
                // 通过Student 实例调用 study 时, study 中的this就是Student 实例
                console.log("我很努力的学习");
            }

        }

        // const s1 = new Student("小明", 15, "初三");
        // s1.speak()


        class Car {
            constructor(name, price) {
                this.name = name;
                this.price = price;
                this.wheel = 4;
            }
            // 类中可以直接写赋值语句
            // 如下代码的含义是 给Car的实例对象添加一个属性 名称为a，值为1
            a = 1
        }

        const c1 = new Car('奔驰c63', 199);
        console.log(c1);


    </script>
</body>
</html>